<?php
/**
 * 我的代金券
 *
 *
 *
 *
 * @copyright  Copyright (c) 2007-2015 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */



defined('ByShopWWI') or exit('Access Invalid!');

class member_signinControl extends mobileMemberControl {

    public function __construct() {
        parent::__construct();
    }

    /**
     * 签到列表
     */
    public function signin_listOp() {
		$condition_arr = $list_log = array();
		$_GET['curpage'] =$_POST['page']+1;
        $condition_arr['pl_memberid'] = $this->member_info['member_id'];
		if(!$condition_arr['pl_memberid']){
			$condition_arr['pl_memberid'] = $_POST['uid'];
		}
		$condition_arr['pl_stage']='signin';
        //分页
        $page   = new Page();
        $points_model = Model('points');
		
        $list = $points_model->getPointsLogList($condition_arr,'','*','');


		foreach($list as $key=>$value){
			$list_log[$key]['sl_id'] = $value['pl_id'];
			$list_log[$key]['sl_memberid'] = $value['pl_memberid'];
			$list_log[$key]['sl_membername'] = $value['pl_membername'];
			$list_log[$key]['sl_addtime'] = $value['pl_addtime'];
			$list_log[$key]['sl_points'] = $value['pl_points'];
			$list_log[$key]['sl_desc'] = $value['pl_desc'];			
			$list_log[$key]['sl_addtime_text'] = date('Y-m-d H:i:s',$value['pl_addtime']);
		}			
        
		$page_count = $points_model->gettotalpage();		
		output_data(array('signin_list' => $list_log),mobile_page($page_count)); 	
    }
	

	/**
     * 检验是否能签到
     */
   	public function checksigninOp() {

		$condition = array();

		$condition['pl_memberid'] = $this -> member_info['member_id'];

		$condition['pl_stage'] = 'signin';

		$todate = date('Ymd000000');

		$totime = strtotime($todate);

		$condition['saddtime'] = $totime;
		$condition['eaddtime'] = $totime+86400;

		$points_model = Model('points');

		$log_array = $points_model -> getPointsInfo($condition);
		$points_signin = intval(C('points_signin'));
		if (!empty($log_array)) {

			//output_error('已签到');
			$type='已签到';
		} else {

			//$points_signin = intval(C('points_signin'));
			$type='未签到';
			//output_data(array('points_signin' => $points_signin));

		}
		output_data(array('points_signin' => $points_signin,'type'=>$type));

	}
	
	/**
     * 签到 array('pl_memberid'=>'会员编号','pl_membername'=>'会员名称','pl_adminid'=>'管理员编号','pl_adminname'=>'管理员名称','pl_points'=>'积分','pl_desc'=>'描述','orderprice'=>'订单金额','order_sn'=>'订单编号','order_id'=>'订单序号','point_ordersn'=>'积分兑换订单编号');
     */
    public function signin_addOp() {
		$points_signin = intval(C('points_signin'));//签到对得积分数
		
		$points_model = Model('points');
		$insertarr['pl_memberid'] = $this->member_info['member_id'];
		$insertarr['pl_membername'] = $this->member_info['member_name'];
		$insertarr['pl_points'] = $points_signin;
		$insertarr['pl_addtime'] = time();
		if($points_signin=='0'){
			output_error('签到积分0');   
		}
		$return = $points_model->savePointsLog('signin',$insertarr,false);
		
        if($return){
			output_data(array('point'=>$points_signin));   
		}else{
			output_error('签到失败');   
		}
	
    }
	/**

     * 红白积分转换信息详细

     */

	public function memberpointsOp(){

        $model_member   = Model('member');
		$red_points=$_POST['red_points'];
		$member_points = $model_member->getMemberInfo(array('member_id'=>$this->member_info['member_id']),'member_red_points,available_predeposit',true);
		//查询红积分数量
		$returnpoints = Model('returnpoint');	
		$order=' order by addtime desc';
	    $repoint=$returnpoints->order($order)->select();
		$repoints=$repoint['back_point'];
		$now_red_points=$member_points['member_red_points'];
		$shengyu_points=$now_red_points-$red_points;
		$pl_addtime=strtotime(date('Y-m-d H:i:s'));
		if($now_red_points >= $red_points){//红积分提现条件:积分大于10000且提现比例为100的倍数
			if($red_points % 100 == 0){
				$red_pointcash=($red_points*$repoints)/100;//红积分提现比例
				$member_array   = array();
				$member_array['available_predeposit']    = $member_points['available_predeposit'] + $red_points/100;////增加余额
				$member_array['member_red_points'] = $member_points['member_red_points']-$red_points;
				$update = $model_member->editMember(array('member_id'=>$this->member_info['member_id']),$member_array);
				//减去红积分
				$memberid=$this->member_info['member_id'];
				$membername=$this->member_info['member_name'];
				//增加余额
				$add_yue=$red_points/100;
				//余额增加日志
				$sql_bc = "insert into shopwwi_pd_log (lg_member_id,lg_member_name,lg_type,lg_av_amount,lg_add_time,lg_desc) values(".$memberid.",".$membername.",'redtocash',". $add_yue .",$pl_addtime,'分红豆变现转入余额-余额增加')";
				mysql_query($sql_bc);
				//积分操作日志
				$back_points=-$red_points;
				$sql_bc="insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values(".$memberid.",'".$membername."',".$back_points.",".$pl_addtime.",'分红豆变现转入余额-分红豆减少','redpoints')";
				mysql_query($sql_bc);
				output_data('1');
			}output_error('积分应为100的倍数');
		}else{
			output_error('积分不足');
		}

	}
	/*消费豆记录*/
	public function consume_pointsOp(){
		$model_member   = Model('member');
		$consume_point = $model_member->getMemberInfo(array('member_id'=>$this->member_info['member_id']),'consume_integral',true);
		$points_model = Model('order');
		
		$list=$points_model->consumeInfo(array('m_id'=>$this->member_info['member_id']));
		
		$data['consume_points']=$consume_point;
		$data['list']=$list;
		output_data($data);
		
	}

}
